package loteria.dao;

import java.util.List;

import loteria.Item;
import loteria.ItemUsuarioNumero;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.engine.query.HQLQueryPlan;

public class LoteriasDAOImplHibernate implements LoteriaDAO {

	private Session session;

	private static LoteriasDAOImplHibernate dao;

	static LoteriaDAO getDAO() {
		if (dao == null) {
			dao = new LoteriasDAOImplHibernate();
		}

		return dao;
	}

	private LoteriasDAOImplHibernate() {
		SessionFactory sessions = new Configuration().configure()
				.buildSessionFactory();
		session = sessions.openSession();
		System.out
				.println("GreetingDAOImplHibernate constructor: session opened");

	}

	@Override
	public void save(Item item) {
		Transaction tx = null;

		try {
			tx = session.beginTransaction();
			session.save(item);
			tx.commit();
			tx = null;
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		}

	}

	@Override
	public Item getItemByNumero(int numero) {
		Item item = null;
		Transaction tx = null;

		try {
			tx = session.beginTransaction();
			item = (Item) session.get(Item.class, numero);
			tx.commit();
			tx = null;
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		}

		return item;
	}

	@Override
	public Item getItemByPremio(int premio) {
		Item item = null;
		Transaction tx = null;

		try {
			tx = session.beginTransaction();
			item = (Item) session.get(Item.class, premio);
			tx.commit();
			tx = null;
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		}

		return item;

	}

	@Override
	@SuppressWarnings(value = "unchecked")
	public List<ItemUsuarioNumero> getItemCantidadByUsuario(String usuario) {
		Query query = session
				.createQuery("from ItemUsuarioNumero where usuario='" + usuario
						+ "'");
		List<ItemUsuarioNumero> results = query.list();

		return results;

	}

	@Override
	public void reiniciarTablas() {
		Transaction tx = null;

		try {
			tx = session.beginTransaction();
			String stringQuery = "DELETE FROM ItemUsuarioNumero";
			Query query = session.createQuery(stringQuery);
			query.executeUpdate();
			tx = null;
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		}
		try {
			tx = session.beginTransaction();
			String stringQuery = "DELETE FROM Item";
			Query query = session.createQuery(stringQuery);
			query.executeUpdate();
			tx = null;
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		}

	}

	@Override
	public void addUsuarioNumero(ItemUsuarioNumero iun) {
		Transaction tx = null;

		try {
			tx = session.beginTransaction();
			session.save(iun);
			tx.commit();
			tx = null;
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		}

	}

}
